package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyOrderItemDao;
import ex.com.jm.food.entity.Order;
import ex.com.jm.food.entity.OrderItem;

/**
 * 企业对订单详情管理
 * @author wangshenfeng
 *
 */
public class CompanyOrderItemDaoImpl extends HibernateDaoSupport implements
		CompanyOrderItemDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<OrderItem> getOrderItems(final int initPage,final int pageRow,
			final String orderId,final String menuName,final String order,final String sort) {
		// TODO Auto-generated method stub
		Object orderItems=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.OrderItemDTO("
						+ "orderItemId,"
						+ "menuId,"
						+ "menuName,"
						+ "menuNowPrice,"
						+ "profit,"
						+ "menuNum"
						+ ") "
						+ "from OrderItem where orderId='"+orderId+"' ";
				if(menuName!=null&&!menuName.equals("")){
					hql+=" and menuName like '%"+menuName+"%'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" and order by "+order+""+sort;
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<OrderItem>) orderItems;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int orderItemCount(final String orderId,final String menuName) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from OrderItem where orderId='"+orderId+"' ";
				if(menuName!=null&&!menuName.equals("")){
					hql+=" and menuName like '%"+menuName+"%'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public void updateOrderItem(OrderItem orderItem) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(orderItem);
	}

	@Override
	public void delOrderItem(String orderItemId) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.getHibernateTemplate().get(OrderItem.class, orderItemId));
	}

	@Override
	public OrderItem editOrderItem(String orderItemId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(OrderItem.class, orderItemId);
	}

	@Override
	public Order getOrder(String orderId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Order.class, orderId);
	}

}
